home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_06 / prince / trigfunc.c < prev    next >
Text File  |  1994-01-24  |  2KB  |  62 lines

  1. /* _Sinf function */
  2. #include "xmath.h"
  3.  
  4. _scos _Sinf(double x)
  5. {
  6. /* compute sinf(x) tanf(x) cosf(x) */
  7.   double xr, den, num2, den2, x2, gr;
  8.   _scos res;
  9.  
  10.   /* Reduce to -PI < x < PI */
  11. #define PI2 M_1_PI*M_1_PI
  12. #define PI3 PI2*M_1_PI
  13. #define PI4 PI3*M_1_PI
  14. #define PI5 PI4*M_1_PI
  15.   xr = .5 * M_1_PI * x;
  16.   gr = 1 / DBL_EPSILON;
  17.   if (FLT_ROUNDS > 0) {
  18.     /* Compiler should eliminate dead code if FLT_ROUNDS is
  19.      * compile time constant */
  20.     if (x < 0) gr = -gr;
  21.     if (FLT_ROUNDS == 1)    /* IEEE std case */
  22.         gr = (xr + gr) - gr;    /* ANINT(x/2/pi) */
  23.     else            /* FLT_ROUNDS 2 or 3 */
  24.         gr = ((xr + (FLT_ROUNDS == 2 ?
  25.                  -.5 : .5)) + gr) - gr;
  26.   } else
  27.     /* Assume FLT_ROUNDS ==0 for positive numbers */
  28.     gr = x < 0 ?
  29.         gr - ((.5 - xr) + gr) : ((xr + .5) + gr) - gr;
  30.  
  31.   xr -= gr;            /* subtract nearest multiple of 2PI */
  32.   /* Range +-2PI reduced to +- .5 */
  33.   x2 = xr * xr;
  34.   /* Rational approx for tan(x/2) = xr/den */
  35.   xr *= 886.77347 * PI4 + x2 * (-99.398953 * PI2 + x2);
  36.   den = 886.77345 * PI5 + x2 *
  37.       (-394.98971 * PI3 + 14.425694 * M_1_PI * x2);
  38.   den2 = den * den;
  39.   num2 = xr * xr;
  40.  
  41.   /* Cos(x) = (1-tan(x/2)^2)/(1+tan(x/2)^2 */
  42.   /* Sin(x) = 2*tan(x/2)/(1+tan(x/2)^2) */
  43.   /* Tan(x) = 2*tan(x/2)/(1-tan(x/2)^2) */
  44.  
  45.   res.c = (den2 - num2) / (den2 + num2);    /* cos */
  46.   res.s = xr * (den + den) / (den2 + num2);    /* sin */
  47.   res.t = xr * (den + den) / (den2 - num2);    /* tan */
  48.   return res;
  49. }
  50.  
  51. float (cosf) (float x) {
  52.   return _Sinf(x).c;
  53. }
  54.  
  55. float (sinf) (float x) {
  56.   return _Sinf(x).s;
  57. }
  58.  
  59. float (tanf) (float x) {
  60.   return _Sinf(x).t;
  61. }
  62.